package cn.life.common.utils.PDF.apply;

import cn.life.business.apply.entity.po.ApplyDetailPo;
import cn.life.business.apply.entity.po.ApplyPo;
import cn.life.common.utils.PDF.PDFBuilder;
import cn.life.common.utils.PDF.PdfElement;
import cn.life.common.utils.PDF.PdfUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author 李佳明
 * @Date 2018-10-09 16:33
 */
public class ApplyTemplate{

    /*public ApplyTemplate(String fileUrl, ApplyPo applyPo) {
        *//**
         * @author  By SuperChen:
         * @version 创建时间：2016年11月21日 下午2:29:51
         * PDFReportTODO
         *//*
        Document document = new Document();// 建立一个Document对象
        try {
            File file = new File(fileUrl);

            String[] folderUrls = fileUrl.split("/");
            int lastLeng = folderUrls[folderUrls.length-1].length();
            String folderUrl = fileUrl.substring(0,fileUrl.length() - lastLeng - 1);
            File folderFile = new File(folderUrl);

            if(!folderFile.exists()){
                folderFile.mkdirs();
            }

            file.createNewFile();

            Rectangle pageSize = new Rectangle(PageSize.A4);
                document.setPageSize(pageSize);
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
                PDFBuilder builder = new PDFBuilder();
                writer.setPageEvent(builder);
                document.open();


                PdfUtil pdfUtil = new PdfUtil();
                BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
                //字体格式
                Font fontChinese11Normal = new Font(bfChinese,11,Font.NORMAL);

                //1.标题
                Paragraph pg_bt = pdfUtil.getHead("朝阳区市容智能审查系统", Element.ALIGN_CENTER,30);
                //document.add(Chunk.NEWLINE);
                document.add(pg_bt);
                //新增蓝色的线
                Paragraph blueLinePar = pdfUtil.getLineStyle(1,90,new BaseColor(144, 205, 247));
                document.add(blueLinePar);
                //document.add(Chunk.NEWLINE);

                //2.主体表格
                Paragraph noPar = pdfUtil.getParagraph("申请票据编号   ： " + applyPo.getApplyNo(),fontChinese11Normal, PdfElement.TITLEINDENTATIONLEFT);
                document.add(noPar);
                document.add(Chunk.NEWLINE);

                Paragraph namePar = pdfUtil.getParagraph("申请票据名称   ： " + applyPo.getApplyName(),fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(namePar);
                document.add(Chunk.NEWLINE);

                Paragraph companyNamePar = pdfUtil.getParagraph("申请单位名称   ： " + applyPo.getApplyCompanyName(),fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(companyNamePar);
                document.add(Chunk.NEWLINE);

                String typeName = "门面装饰";
                if(applyPo.getApplyType() == 2){
                    typeName = "牌匾";
                }
                Paragraph typePar = pdfUtil.getParagraph("申请审查类型   ： " + typeName,fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(typePar);
                document.add(Chunk.NEWLINE);

                Paragraph personPar = pdfUtil.getParagraph("申请单位联系人 ： " + applyPo.getApplyPerson(),fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(personPar);
                document.add(Chunk.NEWLINE);

                Paragraph addrPar = pdfUtil.getParagraph("申请单位地址   ： " + applyPo.getApplyAddr(),fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(addrPar);
                document.add(Chunk.NEWLINE);

                Paragraph phonePar = pdfUtil.getParagraph("申请单位电话   ： " + applyPo.getApplyPhone(),fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(phonePar);
                document.add(Chunk.NEWLINE);

                Paragraph streetPar = pdfUtil.getParagraph("申请单位街道   ： " + applyPo.getApplyStreet(),fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(streetPar);
                document.add(Chunk.NEWLINE);

                Paragraph imgPar = pdfUtil.getParagraph("申请票据图片   ： ",fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(imgPar);
                document.add(Chunk.NEWLINE);

                int firstX = 185;
                int firstY = 380;

                int width = 80;
                int heigt = 100;

                List<ApplyDetailPo> list = applyPo.getDetailList();
                int type = 1;
                int number = 0;
                for(int i=0; i<list.size(); i++){
                    ApplyDetailPo po = list.get(i);
                    if(type == po.getImgType()){
                        String imageUrl = po.getImgUrl();
                        String[] image = imageUrl.split("/");
                        String imgName = image[image.length-1];
                        imageUrl = imageUrl.substring(0,imageUrl.length() - imgName.length());
                        Image image1 = Image.getInstance(imageUrl + java.net.URLEncoder.encode(imgName, "UTF-8"));  //写入图片
                        image1.scaleAbsolute(57, 80);// 控制图片大小 (宽,高)
                        int x = number%4;
                        int y = number/4;
                        image1.setAbsolutePosition(firstX + x * width, firstY - y * heigt);// 控制图片位置
                        document.add(image1);
                        type++;
                        number++;
                    }
                }

                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                document.add(Chunk.NEWLINE);
                Paragraph auditDesc = pdfUtil.getParagraph("街道办审核意见  ： ",fontChinese11Normal,PdfElement.TITLEINDENTATIONLEFT);
                document.add(auditDesc);

                PdfPTable table = new PdfPTable(1); // 3 columns.
                table.setWidthPercentage(100); // Width 100%
                table.setSpacingBefore(10f); // Space before table
                table.setSpacingAfter(10f); // Space after table

                // Set Column widths
                float[] columnWidths = { 1f };
                table.setWidths(columnWidths);

                PdfPCell cell1 = new PdfPCell(new Paragraph(""));
                cell1.setBorderColor(BaseColor.GRAY);
                cell1.setPaddingLeft(300);
                cell1.setMinimumHeight(100);
                cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);
                cell1.setVerticalAlignment(Element.ALIGN_RIGHT);

                table.addCell(cell1);

                document.add(table);

                document.close();
            } catch (Exception e) {
                document.close();
                e.printStackTrace();
            }

    }*/
    public boolean exportPdf(String fileUrl,String timeUrl, ApplyPo applyPo) {

        Document document = new Document();// 建立一个Document对象
        File file = new File(fileUrl);

        float height = 40f;
        float descHeight = 240f;

        try {
            /**
             *  字体
             */
            BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/simhei.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
            // 标题字体
            Font FontChinese18 = new Font(bfChinese, 18,com.itextpdf.text.Font.BOLD);
            // 标题字体
            Font FontChinese99 = new Font(bfChinese, 99,com.itextpdf.text.Font.BOLD);
            Font FontChinese90 = new Font(bfChinese, 90,com.itextpdf.text.Font.BOLD);
            // 字体格式
            Font fontChinese11Normal = new Font(bfChinese,11,Font.NORMAL);
            Font fontChinese13Normal = new Font(bfChinese,13,Font.NORMAL);
            Font fontChinese20Normal = new Font(bfChinese,20,Font.NORMAL);

            file.createNewFile();

            Rectangle pageSize = new Rectangle(PageSize.A4);
            document.setPageSize(pageSize);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            PDFBuilder builder = new PDFBuilder();
            writer.setPageEvent(builder);
            document.open();

            //1.标题
            Paragraph title = new Paragraph("牌匾及门面装饰审查意见表",FontChinese18);
            title.setAlignment(PdfPCell.ALIGN_CENTER);
            title.setSpacingAfter(25);
            document.add(title);

            Paragraph no = new Paragraph("编号：" + applyPo.getApplyNo(),fontChinese11Normal);
            no.setAlignment(PdfPCell.ALIGN_RIGHT);
            no.setSpacingAfter(5);
            document.add(no);

            document.addAuthor("明远科技有限公司");

            /**
             * 初审单位与申请单位行
             */
            PdfPTable table = new PdfPTable(4); // 3 columns.
            table.setWidthPercentage(100); // Width 100%
            table.setSpacingBefore(10f); // Space before table
            table.setSpacingAfter(0); // Space after table

            // Set Column widths
            float[] columnWidths = { 1f, 2f, 1f, 2f };
            table.setWidths(columnWidths);

            PdfPCell cell1 = new PdfPCell(new Paragraph("初审单位：",fontChinese13Normal));
            cell1.setBorderColor(BaseColor.BLACK);//设置单元格边框颜色
            cell1.setMinimumHeight(height);//设置单元格高度
            cell1.setUseAscender(true); //设置可以居中
            cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell2 = new PdfPCell(new Paragraph(""));
            cell2.setBorderColor(BaseColor.BLACK);
            cell2.setMinimumHeight(height);

            PdfPCell cell3 = new PdfPCell(new Paragraph("初审时间：",fontChinese13Normal));
            cell3.setBorderColor(BaseColor.BLACK);
            cell3.setMinimumHeight(height);//设置单元格高度
            cell3.setUseAscender(true); //设置可以居中
            cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell4 = new PdfPCell(new Paragraph(""));
            cell4.setBorderColor(BaseColor.BLACK);

            PdfPCell cell5 = new PdfPCell(new Paragraph("申请单位：",fontChinese13Normal));
            cell5.setBorderColor(BaseColor.BLACK);//设置单元格边框颜色
            cell5.setMinimumHeight(height);//设置单元格高度
            cell5.setUseAscender(true); //设置可以居中
            cell5.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中
            cell5.setPaddingTop(0f);

            PdfPCell cell6 = new PdfPCell(new Paragraph(applyPo.getApplyCompanyName(),fontChinese11Normal));
            cell6.setBorderColor(BaseColor.BLACK);
            cell6.setMinimumHeight(height);
            cell6.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); //设置水平居中
            cell6.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell7 = new PdfPCell(new Paragraph("联系电话：",fontChinese13Normal));
            cell7.setBorderColor(BaseColor.BLACK);
            cell7.setMinimumHeight(height);//设置单元格高度
            cell7.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell7.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell8 = new PdfPCell(new Paragraph(applyPo.getApplyPhone(),fontChinese11Normal));
            cell8.setBorderColor(BaseColor.BLACK);
            cell8.setMinimumHeight(height);//设置单元格高度
            cell8.setUseAscender(true); //设置可以居中
            cell8.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); //设置水平居中
            cell8.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell9 = new PdfPCell();
            cell9.setColspan(4);// 跨4列
            cell9.setMinimumHeight(descHeight);//设置单元格高度
            cell9.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); //设置水平居中
            cell9.setPaddingLeft(12f);
            cell9.setPaddingTop(12f);

            Paragraph firstDesc = new Paragraph("初审意见：", fontChinese13Normal);
            Paragraph date = new Paragraph("                                                         年   月   日",fontChinese13Normal);
            Paragraph blankRow1 = new Paragraph(99f, " ", FontChinese99);
            Paragraph blankRow2 = new Paragraph(85f, " ", FontChinese90);
            cell9.addElement(firstDesc);
            cell9.addElement(blankRow1);
            cell9.addElement(blankRow2);
            cell9.addElement(date);

            /**
             * 终审意见
             */
            PdfPCell cell10 = new PdfPCell(new Paragraph("终审单位：",fontChinese13Normal));
            cell10.setBorderColor(BaseColor.BLACK);//设置单元格边框颜色
            cell10.setMinimumHeight(height);//设置单元格高度
            cell10.setUseAscender(true); //设置可以居中
            cell10.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell10.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell11 = new PdfPCell(new Paragraph(""));
            cell11.setBorderColor(BaseColor.BLACK);
            cell11.setMinimumHeight(height);

            PdfPCell cell12 = new PdfPCell(new Paragraph("终审时间：",fontChinese13Normal));
            cell12.setBorderColor(BaseColor.BLACK);
            cell12.setMinimumHeight(height);//设置单元格高度
            cell12.setUseAscender(true); //设置可以居中
            cell12.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell12.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中

            PdfPCell cell13 = new PdfPCell(new Paragraph(""));
            cell13.setBorderColor(BaseColor.BLACK);
            cell13.setMinimumHeight(height);

            PdfPCell cell14 = new PdfPCell(new Paragraph("终审意见：", fontChinese20Normal));
            cell14.setColspan(4);// 跨4列
            cell14.setMinimumHeight(height);//设置单元格高度
            cell14.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); //设置水平居中
            cell14.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); //设置垂直居中
            cell14.setPaddingLeft(12f);

            PdfPCell cell15 = new PdfPCell();
            cell15.setColspan(4);// 跨4列
            cell15.setMinimumHeight(descHeight);//设置单元格高度
            cell15.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); //设置水平居中
            cell15.setVerticalAlignment(PdfPCell.ALIGN_CENTER); //设置垂直居中

            /*Paragraph endDate = new Paragraph("                                                           年   月   日",fontChinese13Normal);
            Paragraph blankRow3 = new Paragraph(99f, " ", FontChinese99);
            Paragraph blankRow4 = new Paragraph(99f, " ", FontChinese99);
            Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18);
            cell15.addElement(blankRow3);
            cell15.addElement(blankRow4);
            cell15.addElement(blankRow5);
            cell15.addElement(endDate);*/

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.addCell(cell4);
            table.addCell(cell5);
            table.addCell(cell6);
            table.addCell(cell7);
            table.addCell(cell8);
            table.addCell(cell9);
            table.addCell(cell10);
            table.addCell(cell11);
            table.addCell(cell12);
            table.addCell(cell13);
            table.addCell(cell14);
            table.addCell(cell15);

            document.add(table);

            document.close();
        } catch (Exception e) {
            document.close();
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
